﻿using MediatR;
using W.Warehouse.Api.Application.Command.WarehouseCommand;
using W.Warehouse.Domain;
using W.Warehouse.Read.Api;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.WarehouseCommandHandlder
{
    public class WarehouseFanCommandHandlder : IRequestHandler<WarehouseFanCommand, ApiResult<WarehouseInfo>>
    {
        private readonly ILogger<WarehouseFanCommandHandlder> _logger;
        private readonly IWarehouseInfoRepository _warehouseInfoRepository;
        public WarehouseFanCommandHandlder(IWarehouseInfoRepository warehouseInfoRepository, ILogger<WarehouseFanCommandHandlder> logger)
        {
            _warehouseInfoRepository = warehouseInfoRepository;
            _logger = logger;
        }
        public async Task<ApiResult<WarehouseInfo>> Handle(WarehouseFanCommand request, CancellationToken cancellationToken)
        {
            ApiResult<WarehouseInfo> result = new ApiResult<WarehouseInfo>();
            try
            {
                _logger.LogInformation("反填仓库管理成功");
                var res = await _warehouseInfoRepository.GetAsync(x => x.WarehouseId == request.warehouseId);
                result.Data = res.FirstOrDefault();
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
